﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Geometry;
using System.Diagnostics;

namespace TestGeometry {
    public class TestMatrix {
        public static float EPSILON = 1e-3f;

        public void run() {
            this.testInverse();
            testSinglar();
        }

        public void testInverse() {
            Matrix44 m = new Matrix44(
                 1.0f, 0.0f, 1.0f, 2.0f,
                -1.0f, 1.0f, 1.0f, 1.0f,
                 1.0f, -1.0f, 0.0f, 1.0f,
                 1.0f, 1.0f, -1.0f, -2.0f);
            Matrix44 org = new Matrix44(
                 1.0f, -1.0f, -1.0f, 0.0f,
                -1.0f, 2.0f, 2.0f, 1.0f,
                 4.0f, -5.0f, -7.0f, -2.0f,
                -2.0f, 3.0f, 4.0f, 1.0f);

            Matrix44 minv = m.inverse();
            for (int col = 0; col < 4; ++col) {
                for (int row = 0; row < 4; ++row) {
                    float abs = Math.Abs(org.m[row][col] - minv.m[row][col]);
                    Util.assertZero(abs);
                }
            }

            Matrix44 ident = m * minv;
            for (int col = 0; col < 4; ++col) {
                for (int row = 0; row < 4; ++row) {
                    float abs = Math.Abs(ident.m[row][col] -
                        (col == row ? 1 : 0));
                    Util.assertZero(abs);
                }
            }
        }

        public void testSinglar() {
            Matrix44 m = new Matrix44(
                0.0f, 0.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 0.0f, 0.0f);
            try {
                m.inverse();
                Debug.Fail("shoud be singlar");
            } catch (Exception e) {
                Debug.Assert(e.Message == "singlar matrix");
            }
        }
    }
}
